/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.utils;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author ECH
 */
public class MailjetClient {
    
    private static final String APIKey = "22c2046958b31add756de57b93c13afa";
    private static final String SecretKey = "336eec479c0e6ebce04a96bbae5ed000";
    
    public void sendEmail(String fromEmail, String fromName, String toEmail, String subject, String body) {
        Properties props = new Properties ();
        props.put ("mail.smtp.host", "in.mailjet.com");
        props.put ("mail.smtp.socketFactory.port", "465");
        props.put ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put ("mail.smtp.auth", "true");
        props.put ("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance (props,
                new javax.mail.Authenticator ()
                {
                        protected PasswordAuthentication getPasswordAuthentication ()
                        {
                                return new PasswordAuthentication (APIKey, SecretKey);
                        }
                }
        );

        try {
            Message message = new MimeMessage (session);
            message.setFrom (new InternetAddress (fromEmail, fromName));
            message.setRecipients (Message.RecipientType.TO, InternetAddress.parse(toEmail));
            message.setSubject (subject);
            
            message.setContent(body, "text/html; charset=ISO-8859-1");
            Transport.send (message);
        }
        catch (Exception e) {
            throw new RuntimeException (e);
        }
    }
    
    public void sendEmail(String fromEmail, String fromName, String toEmail, String subject, String body, String bccEmail) {
        Properties props = new Properties ();
        props.put ("mail.smtp.host", "in.mailjet.com");
        props.put ("mail.smtp.socketFactory.port", "465");
        props.put ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put ("mail.smtp.auth", "true");
        props.put ("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance (props,
                new javax.mail.Authenticator ()
                {
                        protected PasswordAuthentication getPasswordAuthentication ()
                        {
                                return new PasswordAuthentication (APIKey, SecretKey);
                        }
                }
        );

        try {
            Message message = new MimeMessage (session);
            message.setFrom (new InternetAddress (fromEmail, fromName));
            message.setRecipients (Message.RecipientType.TO, InternetAddress.parse(toEmail));
            //message.addRecipient(RecipientType.BCC, new InternetAddress(bccEmail));
            message.setSubject (subject);
            
            message.setContent(body, "text/html; charset=ISO-8859-1");
            Transport.send (message);
        }
        catch (Exception e) {
            throw new RuntimeException (e);
        }
    }
    
    public static void main (String [] args) {
        MailjetClient mc = new MailjetClient();
        mc.sendEmail("jess@rentpad.me", "Jessica Garcia", "chux18@gmail.com", "Yeaheh Subject", "inner text");
    }
    
}
